//
//  Common.m
//  HollyPhoneSdk
//
//  Created by hollycrm on 2023/3/15.
//

#import "Common.h"
#import "CallOutViewController.h"
@implementation Common


- (UIViewController *)getCurrentViewControllerFromWindow:(UIWindow *)window {
    UIViewController *rootVC = window.rootViewController;
    UIViewController *currentVC = [self getCurrentVCFrom:rootVC];
    return currentVC;
}

- (UIViewController *)getCurrentVCFrom:(UIViewController *)rootVC {
    UIViewController *currentVC = rootVC;
    if ([rootVC isKindOfClass:[UITabBarController class]]) {
        currentVC = [self getCurrentVCFrom:[(UITabBarController *)rootVC selectedViewController]];
    } else if ([rootVC isKindOfClass:[UINavigationController class]]) {
        currentVC = [self getCurrentVCFrom:[(UINavigationController *)rootVC visibleViewController]];
    } else if (rootVC.presentedViewController) {
        currentVC = [self getCurrentVCFrom:rootVC.presentedViewController];
    } else if (rootVC.childViewControllers.count > 0) {
        currentVC = [self getCurrentVCFrom:[rootVC.childViewControllers lastObject]];
    }
    return currentVC;
}

- (UIViewController *)getTopMostViewController {
    UIWindow *mainWindow = [UIApplication sharedApplication].windows[0];
    UIViewController *topViewController = [self getCurrentViewControllerFromWindow:mainWindow];
    
    // 遍历所有窗口
    for (UIWindow *window in [UIApplication sharedApplication].windows) {
        if (!window.isHidden && [window isKindOfClass:[UIWindow class]]) {
            topViewController = [self getCurrentViewControllerFromWindow:window];
            if (topViewController) {
                break;
            }
        }
    }
    return topViewController;
}

//*************************

-(UIViewController *)topMostController{
    UIViewController *rootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
    UIViewController *currentVC = [self getCurrentVCFrom:rootVC];
        return currentVC;
}

-(UIViewController *)restoreRootController{
 
 return [self getTopMostViewController];
 
 
    UIViewController *rootVC = [UIApplication sharedApplication].windows[0].rootViewController;
    UIViewController *currentVC = [self getCurrentVCFrom:rootVC];
    return currentVC;
}

//-(UIViewController *)getCurrentVCFrom:(UIViewController *)rootVC{
//    UIViewController *currentVC;
//        if ([rootVC presentedViewController]) {
//            // 视图是被presented出来的
//            rootVC = [rootVC presentedViewController];
//        }
//        if ([rootVC isKindOfClass:[UITabBarController class]]) {
//            // 根视图为UITabBarController
//            currentVC = [self getCurrentVCFrom:[(UITabBarController *)rootVC selectedViewController]];
//        } else if ([rootVC isKindOfClass:[UINavigationController class]]){
//            // 根视图为UINavigationController
//            currentVC = [self getCurrentVCFrom:[(UINavigationController *)rootVC visibleViewController]];
//        } else {
//            // 根视图为非导航类
//            currentVC = rootVC;
//        }
//    return currentVC;
//}

-(void)goToInitView:(InitViewController *)initview{
    initview.modalPresentationStyle = UIModalPresentationFullScreen;
    UIViewController * rootCon;

    rootCon  = [[Common new] topMostController];
    //NSLog(@"%@",rootCon);
    if (rootCon != nil) {
      //  NSLog(@"%@",@"present");
        [rootCon presentViewController:initview animated:true completion:nil];
    }
}
-(void)goToCallInitView{
 CallOutViewController *vc = [[CallOutViewController alloc] init];
 
 vc.modalPresentationStyle = UIModalPresentationFullScreen;
 UIViewController * rootCon;
    rootCon  = [[Common new] topMostController];
 vc.needInitView = true;
 //NSLog(@"%@",rootCon);
 if (rootCon != nil) {
   //  NSLog(@"%@",@"present");
     [rootCon presentViewController:vc animated:true completion:nil];
 }
}
-(void)goToCallView:(NSString *)callId with:(NSString *)phoneNum isRestore:(Boolean)restore{
    CallOutViewController *vc = [[CallOutViewController alloc] init];
    vc.isRestore = restore;
    vc.modalPresentationStyle = UIModalPresentationFullScreen;
    UIViewController * rootCon;
    if(!restore){
       rootCon  = [[Common new] topMostController];
    }
    else {
        rootCon = [[Common new] restoreRootController];
    }
    
    //NSLog(@"%@",rootCon);
    if (rootCon != nil) {
      //  NSLog(@"%@",@"present");
        [rootCon presentViewController:vc animated:true completion:nil];
    }
}
@end
